package reaction.news.index;

import java.io.IOException;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.queryParser.QueryParser.Operator;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;

import reaction.news.Definitions;

public class QueryIndex {
	
	public static void query(String query) throws CorruptIndexException, IOException, ParseException {
		
		QueryParser queryParser = new QueryParser(org.apache.lucene.util.Version.LUCENE_35,"text", Definitions.analyzer);
	
		Operator op = org.apache.lucene.queryParser.QueryParser.Operator.AND;
		queryParser.setDefaultOperator(op);
		Query q = queryParser.parse(query);
			
		//System.out.println("lucene query:" + q.toString());
			
		TopDocs docs = Definitions.newsSearcher.search(q, 50);
		System.out.println("total docs: " + docs.totalHits);
			
		ScoreDoc[] scoreDocs = docs.scoreDocs;
		
		for (int i = 0; i < scoreDocs.length; i++) {
			System.out.print("rank "+(i+1)+'\t'+scoreDocs[i].score+'\t');
			Document d = Definitions.newsSearcher.doc(scoreDocs[i].doc);
			System.out.println(d.get("name")+" "+d.get("id"));
		}
	}	
}
